package cc.orange.mainView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import cc.orange.R;
import cc.orange.adapter.RecycleBInAdapter;
import cc.orange.base.BaseActivity;
import cc.orange.databinding.ActivityRecycleBinBinding;
import cc.orange.entity.BaseEntity;
import cc.orange.entity.BlockListEntity;
import cc.orange.entity.UpDataPasPostEntity;
import cc.orange.http.ApiService;
import cc.orange.utils.ScreenUtils;
import cc.orange.utils.ZXToastUtil;
import okhttp3.MediaType;
import okhttp3.RequestBody;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.xcheng.retrofit.Call;
import com.xcheng.retrofit.DefaultCallback;
import com.xcheng.retrofit.HttpError;
import com.xcheng.retrofit.RetrofitFactory;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/*回收站*/
public class RecycleBinActivity extends BaseActivity implements View.OnClickListener{

    private ActivityRecycleBinBinding binding;
    private RecycleBInAdapter adapter;
    private List<BlockListEntity.Data>list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_recycle_bin);
        initViews();
        blockList();
    }

    private void initViews() {
        RelativeLayout.LayoutParams pp = new RelativeLayout.
                LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ScreenUtils.getStatusBarHeight(RecycleBinActivity.this));
        binding.msglistTitle.setLayoutParams(pp);
        binding.recycRectclist.setLayoutManager(new LinearLayoutManager(this));
        adapter = new RecycleBInAdapter(list);
        binding.recycRectclist.setAdapter(adapter);
        adapter.setItemClickListener(new RecycleBInAdapter.ItemClickListener() {
            @Override
            public void itemClickListenr(int position) {
                blockRemove(""+list.get(position).getBlockUserId());
            }
        });
        binding.msginfoBack.setOnClickListener(this);
        binding.msgRel2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.msginfo_back:
                finish();
                break;
            case R.id.msg_rel2:
                break;

        }
    }

    private void blockList(){
        RetrofitFactory.create(ApiService.class)
                .blockList(getTokens())
                .enqueue(new DefaultCallback<BlockListEntity>() {
                    @Override
                    public void onStart(Call<BlockListEntity> call) {
                        showLoading_base(RecycleBinActivity.this);
                    }

                    @Override
                    public void onError(Call<BlockListEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<BlockListEntity> call, BlockListEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            list.addAll(baseEntity.getData());
                            adapter.notifyDataSetChanged();
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }

    private void blockRemove(String uid){
        String s = "{\"blockUserId\": \""+uid+"\"}";
        RequestBody requestBody1 = RequestBody.create(MediaType.parse("application/json"), s.toString());


        RetrofitFactory.create(ApiService.class)
                .blockRemove(getTokens(),requestBody1)
                .enqueue(new DefaultCallback<BlockListEntity>() {
                    @Override
                    public void onStart(Call<BlockListEntity> call) {
                        showLoading_base(RecycleBinActivity.this);
                    }

                    @Override
                    public void onError(Call<BlockListEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<BlockListEntity> call, BlockListEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            ZXToastUtil.showToast("恢复成功");
                            list.clear();
                            blockList();

                            EventBus.getDefault().post(new String("home_refres"));
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }
}